package com.pengcheng.taskapplication.mvp.view.activity;


import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.pengcheng.taskapplication.R;
import com.pengcheng.taskapplication.adapter.CarSignAdapter;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.model.BankListModel;
import com.pengcheng.taskapplication.model.CarModel;
import com.pengcheng.taskapplication.model.CarSignModel;
import com.pengcheng.taskapplication.model.UrlItemModel;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenter;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenterV2;
import com.pengcheng.taskapplication.mvp.view.fragment.DialogFragmentCarSignBottom;
import com.pengcheng.taskapplication.mvp.view.widget.MyListView;
import com.pengcheng.taskapplication.net.IView;
import com.pengcheng.taskapplication.util.SPUtils;
import com.pengcheng.taskapplication.util.ToastUtils;

import java.lang.reflect.Type;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class MyCarListGuanjia extends BaseActivity implements IView,DialogFragmentCarSignBottom.OnFragmentInteractionListener{

    @BindView(R.id.pull_to_refresh_lv)
    PullToRefreshListView pull_to_refresh_lv;

    @BindView(R.id.tv_center_title)
    TextView tv_center_title;

    private UserPresenterV2 userPresenterV2;
    private UserPresenter userPresenter;
    ArrayList<CarSignModel> carSignModels= new ArrayList<>();


    ArrayList<CarSignModel> unsignCarSignModels= new ArrayList<>();


    ArrayList<CarSignModel> signCarSignModels= new ArrayList<>();

    private CarSignAdapter adapter;
    private String newToken;
    private String uid;


    @Override
    protected int layoutResID() {
        return R.layout.activity_car_guanjia_layout;
    }

    @Override
    protected void initData() {
        uid= SPUtils.getString(context,ConstantValue.SpType.userid);
        newToken=SPUtils.getString(context,ConstantValue.SpType.newtoken);
        userPresenterV2=new UserPresenterV2(this);
        userPresenter = new UserPresenter(this);
        tv_center_title.setText("我的车辆");
        adapter= new CarSignAdapter(context,carSignModels);
        pull_to_refresh_lv.setAdapter(adapter);
        pull_to_refresh_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

//             CarSignModel carSignModel=(CarSignModel) parent.getAdapter().getItem(position);
//             Intent intent =new Intent(context,ActivityBiaoShi.class);
//             intent.putExtra("model",carSignModel);
//             startActivity(intent);
                //用车牌号去换取url
                CarSignModel carSignModel=(CarSignModel) parent.getAdapter().getItem(position);
                getYuCheBaoSubscribeUrl(carSignModel.getVehicle_id());
            }
        });
        pull_to_refresh_lv.setEmptyView(findViewById(R.id.empty_view));
        getSignCars(newToken,uid);

    }


    public void getYuCheBaoSubscribeUrl(String vehicleId){
        userPresenter.getYuCheBaoSubscribeUrl(vehicleId,ConstantValue.RequestKey.getYuCheBaoSubscribeUrl);
    }

    public void getBankList(String newtoken,String uid){
        userPresenterV2.getBankList(newtoken,uid,ConstantValue.RequestKey.getBankList);
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        if(actionType.equals(ConstantValue.RequestKey.getSignCars)){
            carSignModels.clear();
            carSignModels.addAll((ArrayList<CarSignModel>)data);
            adapter.notifyDataSetChanged();

            unsignCarSignModels.clear();
            signCarSignModels.clear();

            for(CarSignModel carSignModel:carSignModels){
                if(carSignModel.isSign.equals("1")){
                    signCarSignModels.add(carSignModel);
                }else if(carSignModel.isSign.equals("2")){
                    unsignCarSignModels.add(carSignModel);
                }
            }

        }

        if(actionType.equals(ConstantValue.RequestKey.getBankList)){
            Gson gson= new Gson();
            Type type =new TypeToken<BankListModel>(){}.getType();
            BankListModel bankListModel= gson.fromJson(gson.toJson(data),type);


            if(bankListModel.getList().size()!=0){
                //可以签约
                showDialog(unsignCarSignModels);

            }else {
               //
                ToastUtils.showShortToast("请先去绑卡");
            }
        }

        if(actionType.equals(ConstantValue.RequestKey.getYuCheBaoSubscribeUrl)){
            UrlItemModel urlItemModel=(UrlItemModel)data;
            Intent intent =new Intent(context,Html5Activity.class);
            intent.putExtra("url",urlItemModel.getUrl());
            intent.putExtra("title","车架管办理");
            startActivity(intent);
        }
    }

    @Override
    public void onFailed(String code, String msg) {
        ToastUtils.showShortToast(msg);
    }

    @OnClick({R.id.img_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
                //取消签约
            case R.id.ll_unsign:
                finish();
                break;
                //去签约
            case R.id.ll_sign:
                //先判断是否绑卡
                getBankList(newToken,uid);
                break;

        }
    }






    void showDialog(ArrayList<CarSignModel> list) {

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
        if (prev != null) {
            ft.remove(prev);
        }
        ft.addToBackStack(null);

        // Create and show the dialog.
        DialogFragmentCarSignBottom newFragment = DialogFragmentCarSignBottom.newInstance(list);
        newFragment.show(ft, "dialog");
    }

    public void getSignCars(String token,String uid){
        userPresenterV2.getCarSignList(token,uid, ConstantValue.RequestKey.getSignCars);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getSignCars(newToken,uid);
    }

    @Override
    public void onCancel() {

    }

    @Override
    public void onDelete() {

    }

    @Override
    public void onCompete(CarSignModel plateNum) {
        CarSignModel model= plateNum;
        Intent intent =new Intent(context,CarSignAndUnSign.class);
        intent.putExtra("model",model);
        startActivity(intent);
    }
}
